<?php
/**
 * 
 * 订单信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-21
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Order extends CActiveRecord{
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{order}}';
	}
	
	
	/**
	 * 创建订单内容
	 */
	public function createOrder($order_no,$order_info,$user_id){
	
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{order}}(`order_no`,
						`user_id`,
						`order_addr`,
						`payment_id`,
						`express_id`,
						`addr_id`,
						`order_create_time`,
						`order_mark`,`active`) VALUES($order_no,$user_id,'".$order_info['addr']."',1,'".$order_info['express']."','".$order_info['addr']."',NOW(),'".$order_info['message']."',1)";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
	/**
	 * 创建详细订单内容
	 */
	public function createOrderDetails($order_no,$goods_ids,$cart_goods_count,$price_per){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{order_details}}(`order_no`,`goods_id`,`goods_nums`,`order_status_id`,`order_fee`) VALUES('".$order_no."',$goods_ids,$cart_goods_count,6,$cart_goods_count*$price_per)";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 根据用户id查询订单
	 */
	public function getOrderByUserId($user_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.order_create_time,a.order_no,b.goods_nums FROM xy_order a,xy_order_details b WHERE a.order_no=b.order_no  AND  a.user_id=".$user_id." GROUP BY a.order_no";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	/**
	 * 根据用户id分组查询订单数量
	 */
	public function getOrderNumsByUserId($user_id){
		$cmodel = new CCCModel();
		$_sql = "select a.order_create_time,b.order_no,count(b.order_no) as order_nums FROM xy_order a,xy_order_details b WHERE a.order_no=b.order_no AND a.user_id=$user_id group by b.order_no";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	/**
	 * 根据订单号查询商品详细信息
	 */
	public function getGoodsByOrder_no($order_no,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT goods_id,order_status_id FROM {{order_details}}  WHERE goods_id = $goods_id  AND order_no='".$order_no."'";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	
	
	/**
	 * 改变订单状态
	 */
	public function changeOrderStatus($order_no,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{order_details}} SET order_status_id = 0 WHERE goods_id = $goods_id AND  order_no='".$order_no."'";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
	/**
	 * 改变多个商品订单状态
	 */
	public function changeGoodsStatus($order_no){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{order_details}} SET order_status_id = 0 WHERE   order_no='".$order_no."'";
		$row = $cmodel->upd($_sql);
		return $row;
	}

	/**
	 *用户取消订单
	 */
	public function orderCancel($order_no,$goods_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{order_details}} SET order_status_id = 5 WHERE  goods_id = $goods_id AND order_no='".$order_no."'";
		$row = $cmodel->upd($_sql);
		return $row;
	}

	/**
	 *检验是否为合法订单
	 */
	public function checkPay($order_no,$goods_id,$price){
		$cmodel = new CCCModel();
		$_sql = "SELECT goods_id FROM {{order_details}}  WHERE order_fee = $price AND goods_id = $goods_id  AND order_no='".$order_no."'";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	/**
	 *根据订单号查询运费
	 */
	public function getExpressFeeByOrder_no($order_no){
		$cmodel = new CCCModel();
		$_sql = "SELECT a.express_fee FROM {{express}} a,{{order}} b  WHERE a.express_id = b.express_id AND order_no='".$order_no."'";
		$row = $cmodel->sel($_sql);
		return $row;
	}
	
	/**
	 * 清空购物车
	 */
	public function delAllCart($user_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{cart}} SET cart_goods_count = 0,active = 0 WHERE user_id=".$user_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
}
	
?>